import pymongo


class zhihuMongo(object):
    '''
    数据表
        all_user
            uid  name  url  data
        all_url
            uid  name  url
        stay_url
            uid  name  url
        error_data
            uid  name  url  data
        error_url
            uid  name  url

        follow_url
            uid  name  url  follower following
        error_follow
            uid  name  url  follower following follower_index  following_index

    '''
    _instance = 0  # 使用类变量保持操作对象的不变

    def __init__(self):
        self.mongdb = pymongo.MongoClient(host='192.168.122.188', port=27017)
        self.db = self.mongdb.zhihu
        self.all_user = self.db.all_user
        self.all_url = self.db.all_url
        self.stay_url = self.db.stay_url
        self.follow_url = self.db.follow_url

        self.error_follow = self.db.error_follow
        self.error_url = self.db.error_url
        self.error_data = self.db.error_data

    @classmethod
    def instance(cls):
        if cls._instance == 0:
            cls._instance = zhihuMongo()
        return cls._instance

    # 关闭mongo数据库
    def closeConn(self):
        self.mongdb.close()

    ############################  url数据处理 start  ###################################3

    # 插入用户数据
    def insertUserData(self, dic):
        self.all_user.insert_one(dic)

    # 插入url
    def insertAllUrl(self, dic):
        self.all_url.insert_one(dic)

    # 插入待爬取的url
    def insertStayUrl(self, dic):
        self.stay_url.insert_one(dic)



    # 查找url中是否已存在相同id
    def findAllUrlExiste(self, dic):
        uid = dic['uid']
        one_data = self.all_url.find({'uid': uid})
        return one_data

    # 获取待爬去url的一条数据
    def getUrl(self):
        # self.stay_url.aggregate([{$sample:{size:1}}])
        # return self.stay_url.aggregate([{'$sample': {'size': 1}}])
        return self.stay_url.find_one()

    # 删除一条爬取记录
    def removeStayUrl(self, dic):
        uid = dic['uid']
        tmp = self.stay_url.remove({'uid': uid})
        return tmp

    # 获取爬取url表中数据数目
    def getsStayUrlCount(self):
        return self.stay_url.count()

    # 爬去失败的url
    def errorUrl(self, dic):
        self.error_url.insert_one(dic)

    # 插入失败的数据
    def errorData(self, dic):
        self.error_data.insert_one(dic)

    def removeData(self):
        self.all_user.remove()
        self.all_url.remove()
        # self.stay_url.remove()
        self.stay_url.remove()

    ############################  url数据处理 end    ###################################3
    ############################  关注数据处理 start  ###################################3
    # 获取关注的url
    def getFollow(self):
        return self.follow_url.find_one()

    # 删除关注的url
    def removeFollow(self, dic):
        uid = dic['uid']
        tmp = self.follow_url.remove({'uid': uid})
        return tmp

    # 获取关注表中数据数目
    def getFollowCount(self):
        return self.follow_url.count()

    def insertFollowUrl(self, dic):
        self.follow_url.insert_one(dic)

    # 插入关注的错误数据
    def errorFollow(self, dic):
        self.error_follow.insert_one(dic)

    ############################  关注数据处理 end   ###################################3


# ======================================================================================================
# 插入用户数据
def INSERT_USER_DATA(dic):
    zhihuMongo.instance().insertUserData(dic)


# 插入url数据, 判断数据是否存在，然后插入
def INSERT_ALL_URL(dic):
    one_data = zhihuMongo.instance().findAllUrlExiste(dic)
    if one_data.count() == 0:
        zhihuMongo.instance().insertStayUrl(dic)
        zhihuMongo.instance().insertAllUrl(dic)
    else:
        print('插入的数据已存在', dic)

# 插入 关注用户的url
def INSERT_FOLLOW_URL(dic):
    zhihuMongo.instance().insertFollowUrl(dic)


# 获取一条url
def GET_STAY_URL():
    one_data = zhihuMongo.instance().getUrl()
    tmp = zhihuMongo.instance().removeStayUrl(one_data)
    print('GET_URL > remove_stay_url', tmp)
    return one_data


# 获取一条 关注url
def GET_FOLLOW_URL():
    one_data = zhihuMongo.instance().getFollow()
    tmp = zhihuMongo.instance().removeFollow(one_data)
    print('GET_URL > remove_follow_url', one_data, tmp)
    return one_data


# 获取一条url
def GET_STAY_URL_COUNT():
    return zhihuMongo.instance().getsStayUrlCount()


# 获取一条url
def GET_FOLLOW_URL_COUNT():
    return zhihuMongo.instance().getFollowCount()

#
def ERROR_FOLLOW_URL(dic):
    zhihuMongo.instance().errorFollow(dic)


# 无法请求的url
def ERROR_URL(dic):
    zhihuMongo.instance().errorUrl(dic)


# 无法解析的数据
def ERROR_DATA(dic):
    zhihuMongo.instance().errorData(dic)


def REMOVE_DATA():
    zhihuMongo.instance().removeData()
